package com.zjdiepu.www.biz.third;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.third.AliDeliverInfoMapper;
import com.zjdiepu.www.ibiz.third.IAliDeliverInfoService;
import com.zjdiepu.www.model.third.dto.AliDeliverInfo;
import com.zjdiepu.www.model.third.ext.AliDeliverInfoExt;

/**
 * 快递信息Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月18日 14时15分<br/>
 * @since 1.0<br/>
 */
@Service
public class AliDeliverInfoServiceImpl extends BaseService implements IAliDeliverInfoService {
	public static final Logger logger = LoggerFactory.getLogger(AliDeliverInfoServiceImpl.class);

	@Autowired
	private AliDeliverInfoMapper aliDeliverInfoMapper;
	
	@Override
	public void saveUpdateAndDelete(AliDeliverInfo aliDeliverInfo, OperateType operType) {
		switch (operType) {
		case add:
			aliDeliverInfo.initInsertData(aliDeliverInfo.getModifier(), aliDeliverInfo.getModifierName());
			aliDeliverInfoMapper.insert(aliDeliverInfo);
			break;
		case edit:
			aliDeliverInfo.initUpdateData(aliDeliverInfo.getModifier(), aliDeliverInfo.getModifierName());
			// 影响的记录数
			int updateCount = aliDeliverInfoMapper.updateByPrimaryKeySelective(aliDeliverInfo);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			aliDeliverInfo.initDeleteData(aliDeliverInfo.getModifier(), aliDeliverInfo.getModifierName());
			// 影响的记录数
			int deleteCount = aliDeliverInfoMapper.updateByPrimaryKeySelective(aliDeliverInfo);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<AliDeliverInfo> selectByExt(AliDeliverInfoExt aliDeliverInfoExt) {
		return aliDeliverInfoMapper.selectByExt(aliDeliverInfoExt);
	}
	
	@Override
	public int countByExt(AliDeliverInfoExt aliDeliverInfoExt){
		return aliDeliverInfoMapper.countByExt(aliDeliverInfoExt);
	}
	
	@Override
	public AliDeliverInfo selectById(String id) {
		return aliDeliverInfoMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			aliDeliverInfoMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(AliDeliverInfo aliDeliverInfo) {
		return aliDeliverInfoMapper.countRefined(aliDeliverInfo);
	}

	@Override
	public AliDeliverInfo selectRefined(AliDeliverInfo aliDeliverInfo) {
		return aliDeliverInfoMapper.selectRefined(aliDeliverInfo);
	}

	@Override
	public boolean repeat(AliDeliverInfo aliDeliverInfo) {
		int count = countRefined(aliDeliverInfo);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(aliDeliverInfo.getId())) {
			return true;
		}
		AliDeliverInfo dbItem = selectRefined(aliDeliverInfo);
		if(null == dbItem) {
			return false;
		}
		return !aliDeliverInfo.getId().equals(dbItem.getId());
	}

	@Override
	public boolean deleteByNu(String nu) {
		aliDeliverInfoMapper.deleteByNu(nu);
		return true;
	}

	@Override
	public AliDeliverInfo selectByNu(String nu) {
		return aliDeliverInfoMapper.selectByNu(nu);
	}
}
